package org.anupta.game.server.game.packet.account.character.create;

import org.anupta.game.entity.dao.DAO;
import org.anupta.game.entity.dao.PlayerDAO;
import org.anupta.game.entity.entities.Account;
import org.anupta.game.entity.entities.Player;
import org.anupta.game.entity.entities.Player.Classe;
import org.anupta.game.entity.entities.Player.Sexe;
import org.anupta.game.server.game.client.GameClient;
import org.anupta.game.server.game.packet.GamePacket;
import org.anupta.game.server.game.packet.account.AccountList;
import org.anupta.game.server.game.packet.account.character.create.answer.CharacterCreationFailed;
import org.anupta.game.server.game.packet.account.character.create.answer.CharacterCreationSucced;
import org.anupta.game.server.game.packet.account.character.create.answer.MaxCharacter;
import org.anupta.game.server.game.packet.account.character.create.answer.NameExist;
import org.anupta.game.utils.others.Config;

public class CharacterCreate extends GamePacket {
	private int guid = 0;
	private String name = null;
	private Classe classe = null;
	private Sexe sexe = null;
	private int[] colors = {0,0,0};
	
	public CharacterCreate(GameClient client, String message) {
		super(client, message);
		unpack();
		doPacket();
	}

	@Override
	public void unpack() {
		String[] infos = packetIn.substring(2).split("\\|");
		guid = PlayerDAO.getNewGuid();
		name = infos[0];
		classe = Classe.get(Byte.parseByte(infos[1]));
		sexe = Sexe.get(Byte.parseByte(infos[2]));
		colors[0] = Integer.parseInt(infos[3]);
		colors[1] = Integer.parseInt(infos[4]);
		colors[2] = Integer.parseInt(infos[5]);
	}

	@Override
	public void pack() {
	}

	@Override
	public void doPacket() {
		if(client.getAccount().getPlayers().size() == Config.MAX_CHARAC){
			new MaxCharacter(client);
			return;
		}
		if(DAO.getDao(Player.class).find(name) != null){
			new NameExist(client);
			return;
		}
		if(name.contains("mj") || name.contains("modo") || name.contains("admin")){
			new CharacterCreationFailed(client);
			return;
		}
		int tCount = 0;
		char a = ' ';
		char b = ' ';
		for(char c : name.toLowerCase().toCharArray()){
			if(c == '-'){
				if(tCount == 1){
					new NameExist(client);
					return;
				}
				tCount++;
			}
			if(!((c >= 'a' && c <= 'z') || c == '-'))
			{
				new NameExist(client);
				return;
			}
			if(c == a && c == b)
			{
				new NameExist(client);
				return;
			}
			if(c >= 'a' && c <= 'z')
			{
				a = b;
				b = c;
			}
		}
		Player player = new Player(guid,
								   name,
								   Config.ID,
								   sexe.getSexe(),
								   classe.getId(),
								   1,
								   Player.getDefautGfx(classe.getId(), sexe.getSexe()),
								   colors[0],
								   colors[1],
								   colors[2],
								   true,
								   "",
								   100,
								   "i*#$p%!?:"
								   );
		client.getAccount().addPlayer(player);
		DAO.getDao(Player.class).create(player);
		DAO.getDao(Account.class).update(client.getAccount());
		new CharacterCreationSucced(client);
		new AccountList(client);
	}
}
